<?php

/**
 * Class member_levelModel
 * 会员等级管理
 * @version 1.0
 */
defined('InShopNC') or exit('Access Invalid!');
class member_levelModel extends Model{
    public function __construct(){
        parent::__construct('member_level');
    }

    /**
     * 获取用户等级列表
     * @return array
     */
    public function getLevelList(){
        $cache=Cache::getInstance(c('cache.type'));
        $levels=$cache->get('member_level');
        if($levels){
            $levels=is_string($levels)?unserialize($levels):$levels;
        }else{
            $lists=$this->order(array('min_points'=>'ASC','member_level_id'=>'ASC'))->select();
            $levels=array();
            foreach($lists as $item){
                $levels[$item['member_level_id']]=$item;
            }
           $cache->set('member_level',c('cache.type')=='file'?var_export($levels,true):serialize($levels));
        }
        return $levels;
    }

    /**
     * 新增会员等级
     * @param $data
     * @param null $error
     * @return bool|mixed
     */
    public function addLevel($data,&$error=null){
        $levels=$this->getLevelList();
        foreach($levels as $_item){
            if($data['level_name']==$_item['level_name']){
                return false;
            }
        }
        $flag=$this->insert($data);
        if($flag){
            $this->cacheLevel();
        }
        return $flag;
    }

    /**
     * 获取会员等级描述
     * @param $levelId
     * @return false|array
     */
    public function getLevel($levelId){
        $levels=$this->getLevelList();
        if(isset($levels[$levelId])){
            return $levels[$levelId];
        }
        return false;
    }

    /**
     * 删除会员等级
     * @param $levelId
     * @return bool
     */
    public function delLevel($levelId){
        $flag=$this->delete(array('where'=>array('member_level_id'=>is_array($levelId)?array('in',$levelId):(int)$levelId)));
        if($flag){
            $this->cacheLevel();
        }
        return $flag;
    }

    /**
     * 缓存用户等级信息
     * @return bool
     */
    public function cacheLevel(){
        $lists=$this->select();
        $levels=array();
        foreach($lists as $item){
            $levels[$item['member_level_id']]=$item;
        }
        $cache=Cache::getInstance(c('cache.type'));
        if(c('cache.type')=='file'){
            return $cache->set('member_level',$levels);
        }
        return $cache->set('member_level',c('cache.type')=='file'?var_export($levels,true):serialize($levels));
    }

    /**
     * 获取用户会员等级描述
     * @param $memberId
     * @return array|false
     */
    public function getMemberLevel($memberId){
        $member=$this->table('member')->field('member_level')->find(array('where'=>array('member_id'=>$memberId)));
        return $member?$this->getLevel($member['member_level']):false;
    }

    /**
     * 设置用户等级
     * @param $memberId
     * @param $levelId
     * @return bool
     */
    public function setUserLevel($memberId,$levelId){
        $hasMap=array('where'=>array('member_id'=>$memberId));
        $member=$this->table('member')->field('member_level')->find($hasMap);
        if($member&&$this->getLevel($levelId)){
            return $this->table('member')->update(array('member_level'=>$levelId),$hasMap);
        }
        return false;
    }
}